<?php

/**
 * kilometraje actions.
 *
 * @package    cronos-doctrine
 * @subpackage kilometraje
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class kilometrajeActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $this->kilometrajes = Doctrine_Core::getTable('Kilometraje')
      ->createQuery('a');
    
    $this->pager = new sfDoctrinePager('Kilometraje', sfConfig::get('app_cliente_x_pag'));
    $this->pager->setQuery($this->kilometrajes);
    $this->pager->setPage($request->getParameter('page', 1));
    $this->pager->init();
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new KilometrajeForm();
            
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new KilometrajeForm();

    $this->processForm($request, $this->form);
    $this->getUser()->setFlash('error', 'No se pudo Crear el item, existen errores en el formulario.\n Por favor revisar campos resaltado'); 
    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($kilometraje = Doctrine_Core::getTable('Kilometraje')->find(array($request->getParameter('id'))), sprintf('Object kilometraje does not exist (%s).', $request->getParameter('id')));
    $this->form = new KilometrajeForm($kilometraje);
            
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($kilometraje = Doctrine_Core::getTable('Kilometraje')->find(array($request->getParameter('id'))), sprintf('Object kilometraje does not exist (%s).', $request->getParameter('id')));
    $this->form = new KilometrajeForm($kilometraje);

    $this->processForm($request, $this->form);
    $this->getUser()->setFlash('error', 'No se pudo Crear el item, existen errores en el formulario.\n Por favor revisar campos resaltado'); 
    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($kilometraje = Doctrine_Core::getTable('Kilometraje')->find(array($request->getParameter('id'))), sprintf('Object kilometraje does not exist (%s).', $request->getParameter('id')));
    $kilometraje->delete();

    $this->redirect('kilometraje/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $notice = $form->getObject()->isNew() ? 'El item se creó correctamente.' : 'El item se actualizó correctamente.';  
        
        try {
            $kilometraje = $form->save();
            
        } catch (Doctrine_Validator_Exception $e) {

        $errorStack = $form->getObject()->getErrorStack();
        $message = get_class($form->getObject()) . ' has ' . count($errorStack) . " field" . (count($errorStack) > 1 ?  's' : null) . " with validation errors: ";
        foreach ($errorStack as $field => $errors) {
            $message .= "$field (" . implode(", ", $errors) . "), ";
        }
        $message = trim($message, ', ');

        $this->getUser()->setFlash('error', $message);
        return sfView::SUCCESS;
      }
           
      $this->getUser()->setFlash('notice', $notice);
      $this->redirect('kilometraje/edit?id='.$kilometraje->getId());
    }
  }
}
